package org.jeecgframework.web.service.impl;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.service.ZyCommonService;
import org.jeecgframework.web.service.ZyEngineerServiceI;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.pojo.base.TSRole;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.pojo.base.ZyEngineerEntity;
import org.jeecgframework.web.system.pojo.base.ZyInspectionPlanEntity;
import org.jeecgframework.web.util.StaticValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.io.Serializable;

@Service("zyEngineerService")
@Transactional
public class ZyEngineerServiceImpl extends CommonServiceImpl implements ZyEngineerServiceI {

	@Autowired
	private ZyCommonService zyCommonService;
	
 	public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
		this.doDelSql((ZyEngineerEntity)entity);
 	}
 	
 	public <T> Serializable save(T entity) {
 		Serializable t = super.save(entity);
 		//执行新增操作配置的sql增强
// 		this.doAddSql((ZyEngineerEntity)entity);
 		return t;
 	}
 	
 	public <T> void saveOrUpdate(T entity) {
 		super.saveOrUpdate(entity);
 		//执行更新操作配置的sql增强
// 		this.doUpdateSql((ZyEngineerEntity)entity);
 	}
 	
 	/**
	 * 默认按钮-sql增强-新增操作
	 * @param id
	 * @return
	 */
 	public boolean doAddSql(ZyEngineerEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作
	 * @param id
	 * @return
	 */
 	public boolean doUpdateSql(ZyEngineerEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作
	 * @param id
	 * @return
	 */
 	public boolean doDelSql(ZyEngineerEntity t){
	 	return true;
 	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @return
	 */
 	public String replaceVal(String sql,ZyEngineerEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{name}",String.valueOf(t.getName()));
 		sql  = sql.replace("#{gender}",String.valueOf(t.getGender()));
 		sql  = sql.replace("#{cellphone}",String.valueOf(t.getCellphone()));
 		sql  = sql.replace("#{email}",String.valueOf(t.getEmail()));
 		sql  = sql.replace("#{belong_org}",String.valueOf(t.getBelongOrg()));
 		sql  = sql.replace("#{state}",String.valueOf(t.getState()));
 		sql  = sql.replace("#{authentication}",String.valueOf(t.getAuthentication()));
 		sql  = sql.replace("#{parent_id}",String.valueOf(t.getParentId()));
 		sql  = sql.replace("#{id_num}",String.valueOf(t.getIdNum()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}

	@Override
	public void batchUpdateEngineerWhenUpdateTsUser(TSUser tsuser, String roleIds, String departIds) {
		Boolean isAeg = true,oneOfThis = false;
		List<String> roleIdList = new ArrayList<String>();
		if(StringUtil.isNotEmpty(roleIds)){
			for(String roleId : roleIds.split(",")){
				if(StringUtil.isNotEmpty(roleId)){
					roleIdList.add(roleId);
				}
			}
			TSRole roleAeg = super.findUniqueByProperty(TSRole.class, "roleCode", StaticValue.RULE_CODE_AREA_ENGINEER);
			TSRole roleEg = super.findUniqueByProperty(TSRole.class, "roleCode", StaticValue.RULE_CODE_THIRDPARTY_ENGINEER);
			if(roleIdList.contains(roleAeg.getId())){
				isAeg = true;
				oneOfThis = true;
				if(StringUtil.isNotEmpty(departIds)){
					String[] departIdsArr = departIds.split(",");
					if(null != departIdsArr && departIdsArr.length > 0){
						for (String departId : departIdsArr) {
							if(StringUtil.isNotEmpty(departId)){
								TSDepart depart = super.getEntity(TSDepart.class, departId);
								/*List<ZyInspectionPlanEntity> ips = super.findByProperty(ZyInspectionPlanEntity.class, "company", depart.getOrgCode());
								if(null != ips && ips.size() > 0){
									for (ZyInspectionPlanEntity ip : ips) {
										if(ip.getCompanyHadAreaEg().equals("0")){//如果之前巡检单位没有区域工程师
											if(ip.getEngineer() != null && ip.getState().equals() && 
													ip.getEngineer().getTsBaseUser().getId().equals(tsuser.getId())){//如果当前巡检的工程师关联的系统用户和编辑的用户是一个
												ip.setCompanyHadAreaEg("1");//设置当前巡检单位有区域工程师
												ip.setAreaEngineer(ip.getEngineer());//把工程师转移到区域工程师
												ip.setEngineer(null);//把工程师设置成null
											}
										}
									}
								}*/
								String sql = "UPDATE zy_inspection_plan SET company_had_area_eg = 1 WHERE state = ? AND company = ?";
								super.executeSql(sql, StaticValue.DIC_PLAN_STATE_SEND_TO_THIRDPARTY, depart.getOrgCode());//更新巡检计划中状态为 已分配到第三方 ,当前巡检单位的 “是否已有区域工程师” 字段
							}
						}
					}
				}
			}else if(roleIdList.contains(roleEg.getId())){
				isAeg = false;
				oneOfThis = true;
			}
		}
		if(oneOfThis){
			List<ZyEngineerEntity> egs = super.findByProperty(ZyEngineerEntity.class, "tsBaseUser.id", tsuser.getId());
			if(null != egs && egs.size() > 0){
				for (ZyEngineerEntity eg : egs) {
					//如果原来是区域工程师，降为 工程师 //如果原来是工程师，升为 区域工程师
					if((eg.getIsAreaEngineer() && !isAeg) || (!eg.getIsAreaEngineer() && isAeg)){
						eg.setParentId(null);
					}
					eg.setIsAreaEngineer(isAeg);
				}
				super.batchUpdate(egs);//更新
				
				if(!isAeg){//更新工程师之后判断当前机构下是否还存在区域工程师
					if(StringUtil.isNotEmpty(departIds)){
						String[] departIdsArr = departIds.split(",");
						if(null != departIdsArr && departIdsArr.length > 0){
							for (String departId : departIdsArr) {
								if(StringUtil.isNotEmpty(departId)){
									TSDepart depart = super.getEntity(TSDepart.class, departId);
									if(zyCommonService.judgeCompanyHadAreaEnginee(depart.getOrgCode(),null)){//如果更新之后没有区域工程师
										String sql = "UPDATE zy_inspection_plan SET company_had_area_eg = 0 WHERE state = ? AND company = ?";
										super.executeSql(sql, StaticValue.DIC_PLAN_STATE_SEND_TO_THIRDPARTY, depart.getOrgCode());//更新巡检计划中状态为 已分配到第三方,当前巡检单位的 “是否已有区域工程师” 字段
									}
								}
							}
						}
					}
				}
			}
		}
	}
	
	
	
}